  <?php

class mysqldb{
    
    public $con;
    public $error;
    public function __construct($host="localhost",$name="root",$pwd="root",$dbname="ai",$charset="utf8"){
        $this->connect($host,$name,$pwd);
        $this->selectDb($dbname);
        $this->chartset($charset);
   
    }
    public function connect($host="localhost",$name="root",$pwd="root"){
        $this->con = mysqli_connect($host,$name,$pwd);
    
    }
    public function selectdb($dbname){
        mysqli_select_db($this->con, $dbname);
    }
    public function chartset($chartset="utf8"){
        mysqli_set_charset($this->con, $chartset);
    }    
     public function select($sql){
//        echo $sql."查询语句";
         $result=mysqli_query($this->con,$sql);
         if($result==null){
             $this->error = mysqli_error($this->con);
             return array();
         }
        $arr = array();//接收查询的记录
        while($row = mysqli_fetch_assoc($result)) {
        $arr[] = $row;
}
         return $arr;     
    }
    public function select1($sql){
        if($sql==null){
            return array();
        }
        $result = mysqli_query($this->con,$sql);
        if(!$result){
            $this->error=mysqli_error();
            return array();
        }
        $arr = mysqli_fetch_assoc($result);
        return $arr;
    }
    public function selectupdate($sql) {//输入查询sql语句
        if(empty($sql)) {
             $a = "请输入语句";
             return $a;
        }
        $result = mysqli_query($this->con,$sql);
        if(!$result){
             $this->error = mysqli_error($this->con);
                 return $this->error;
        }
       $arr = mysqli_fetch_assoc($result);
        return $arr;
    }
    public function insert($sql){
     $result = mysqli_query($this->con,$sql);    
        if(!$result){
            $this->error=mysqli_error();
            return false;
        }else{
            return true;
        }
        
    }
    public function insert1($sql){
        if(empty($sql)){
            echo "发帖失败";
        }
         $result = mysqli_query($this->con,$sql);
         if(!$result){
             $result=mysqli_error($this->con);
             return $result;
         }
     return 1;
        
    }
    public function delete($sql){
        if(empty($sql)) {
         return 0;
        }
             $result = mysqli_query($this->con,$sql);
             if(!$result){
                 $error = mysqli_error($this->con);
                 return $error;
             }
             return 1;
    }
    public function update($sql) {
                 if(empty($sql)) {
                 $a = "请输入语句";
             return $a;
        }
             $res = mysqli_query($this->con,$sql);
             if(!$res){
                 $error = mysqli_error($this->con);
                 return $error;
             }
             return 1;
         }
         public function update1($sql) {
                 if(empty($sql)) {
                 $a = "请输入修改内容";
             return $a;
        }
             $res = mysqli_query($this->con,$sql);
             if(!$res){
                 $error = mysqli_error($this->con);
                 return $error;
             }
             return 1;
         }
    public function denglu($sql){
//        echo $sql."查询语句";
         $result=mysqli_query($this->con,$sql);
         if($result==null){
             $this->error = mysqli_error($this->con);
                    return array();
         }
//        $row = mysqli_fetch_assoc($result);
        $res = mysqli_num_rows($result);
        if($res==1){
        return true;
    }
} 
     public function __destruct() {
        mysqli_close($this->con);
    }
    
    
}



